﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using Coding4Fun.Cannon.Phone.Service;

namespace Coding4Fun.Cannon.Phone.Controls
{
	public partial class StatusView : UserControl
	{
		public StatusView()
		{
			InitializeComponent();
		}

		private void UserControl_Loaded(object sender, RoutedEventArgs e)
		{
			CheckNetworkStatus();
		}

		private void CheckNetworkStatus()
		{
			// updated when we warm up the server
			// since async call, turn off first
			chkCannonStatusLight.IsChecked = false;
			chkNetworkStatusLight.IsChecked = NetworkInterface.GetIsNetworkAvailable();
			WarmUpServer();
		}

		private void WarmUpServer()
		{
			if (App.Service != null)
				App.Service.Init(InitCompleted);
		}

		private void InitCompleted(object sender, ServiceCompletedEventArgs e)
		{
			Dispatcher.BeginInvoke(() => UpdateUiAfterInit(e));
		}

		private void UpdateUiAfterInit(ServiceCompletedEventArgs e)
		{
			var hasResult = !(string.IsNullOrEmpty(e.Result) || e.ServiceException != null);
			chkCannonStatusLight.IsChecked = hasResult;

			if (string.IsNullOrEmpty(e.Result))
				return;

			//@"<result><PanMin>{0}</PanMin><PanMax>{1}</PanMax><TiltMin>{2}</TiltMin><TiltMax>{3}</TiltMax></result>"
			var doc = XDocument.Load(new StringReader(e.Result));

			if (doc.Document == null)
				return;

			var root = doc.Document.Element("Results");

			if (root == null)
				return;

			var panMinXml = root.Element("PanMin");
			var panMaxXml = root.Element("PanMax");
			var tiltMinXml = root.Element("TiltMin");
			var tiltMaxXml = root.Element("TiltMax");

			var panMin = 0;
			var panMax = 0;
			var tiltMin = 0;
			var tiltMax = 0;

			if (panMinXml != null)
				int.TryParse(panMinXml.Value, out panMin);

			if (panMaxXml != null)
				int.TryParse(panMaxXml.Value, out panMax);

			if (tiltMinXml != null)
				int.TryParse(tiltMinXml.Value, out tiltMin);

			if (tiltMaxXml != null)
				int.TryParse(tiltMaxXml.Value, out tiltMax);

			App.ApplicationSettings.PanMinimum = panMin;
			App.ApplicationSettings.PanMaximum = panMax;
			App.ApplicationSettings.TiltMinimum = tiltMin;
			App.ApplicationSettings.TiltMaximum = tiltMax;
		}

		private void Ping_Click(object sender, RoutedEventArgs e)
		{
			//Switch to real service if not using real service
			if (App.Service.GetType() != typeof(CannonService))
				App.Service = new CannonService(App.ApplicationSettings);

			CheckNetworkStatus();
		}

		private void ShowOptions_Click(object sender, RoutedEventArgs e)
		{
			var settings = new SettingsView();
			settings.Finished += settings_Finished;
			LayoutRoot.Children.Add(settings);
		}

		void settings_Finished(object sender, EventArgs e)
		{
			LayoutRoot.Children.RemoveAt(LayoutRoot.Children.Count - 1);
		}
	}
}
